<template>
  <div class="outmain ba_f">
    <div class="ba_f">
      <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
        <el-tab-pane label="预约订单" name="storeappointmentlist"></el-tab-pane>
        <el-tab-pane label="全部订单" name="storeappointmentorder"></el-tab-pane>
        <el-tab-pane label="预约设置" name="storeappointmentstoreset"></el-tab-pane>
      </el-tabs>
    </div>
    <div class="ba_f">
      <div class="shuju_title mar_b20 mar_t20">
        <div class="shuju_title_text">
          <span>预约设置</span>
        </div>
      </div>
      <el-form ref="form" :model="form" label-width="180px">
        <el-form-item label="可提前预约天数">
          <el-input-number v-model="form.days" :min="1" :max="100" step-strictly></el-input-number>
          <span class="mar_l10">天</span>
          <div class="yb_margin lh16">可提前预约天数，单位:天</div>
        </el-form-item>
        <el-form-item label="预约时间段">
          <el-time-picker is-range format="HH:mm" v-model="time" range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间" placeholder="选择时间范围"></el-time-picker>
        </el-form-item>
        <el-form-item label="间隔时段细分">
          <el-radio v-model="form.interval" label="15">15分钟</el-radio>
          <el-radio v-model="form.interval" label="30">30分钟</el-radio>
          <el-radio v-model="form.interval" label="60">1小时</el-radio>
        </el-form-item>
        <el-form-item label="可预约星期">
          <el-checkbox-group v-model="form.week">
            <el-checkbox label="1">星期一</el-checkbox>
            <el-checkbox label="2">星期二</el-checkbox>
            <el-checkbox label="3">星期三</el-checkbox>
            <el-checkbox label="4">星期四</el-checkbox>
            <el-checkbox label="5">星期五</el-checkbox>
            <el-checkbox label="6">星期六</el-checkbox>
            <el-checkbox label="7">星期天</el-checkbox>
          </el-checkbox-group>
          <div class="yb_margin lh16">勾选则表示用户可预约</div>
        </el-form-item>
        <el-form-item label="提前预约天数">
          <el-radio v-model="form.selectDay" label="1">今日</el-radio>
          <el-radio v-model="form.selectDay" label="2">明日</el-radio>
          <div class="yb_margin lh16">如选择明日则预约时间从明天开始</div>
        </el-form-item>
        <el-form-item label="取消订单时间">
          <el-input-number v-model="form.cancelTime" :min="1" :max="1000" step-strictly></el-input-number>
          <div class="yb_margin lh16">设置提前多少分钟用户可以取消订单  单位：分钟</div>
        </el-form-item>
        <el-form-item label="人数选项最小值">
          <el-input-number v-model="form.minNum" :min="1" :max="1000" step-strictly></el-input-number>
          <div class="yb_margin lh16">如设置为1,则表示预约人数起始值从1开始</div>
        </el-form-item>
        <el-form-item label="人数选项最大值">
          <el-input-number v-model="form.maxNum" :min="1" :max="1000" step-strictly></el-input-number>
          <div class="yb_margin lh16">如设置为30,则表示预约人数最大值为30人</div>
        </el-form-item>
        <el-form-item label="自动确认">
          <el-switch v-model="form.auto" active-text="开启" inactive-text="关闭" active-value="1" inactive-value="2"></el-switch>
          <div class="yb_margin lh16">开启后,用户预约系统自动确认预约成功</div>
        </el-form-item>
        <el-form-item label="预约公告">
          <el-input v-model="form.notice" placeholder="请输入预约公告"></el-input>
        </el-form-item>
        <el-form-item label="定金说明">
          <baidu-ueditor :content="form.introduction" @ueditor="getUeditor"></baidu-ueditor>
        </el-form-item>
      </el-form>
    </div>
    <div class="mar_t20 ba_f pad_20 t_c">
      <el-button type="primary" @click="submitForm">保存</el-button>
    </div>
  </div>
</template>
<script>
import { getStoreConfig, postStoreConfig } from '@/api/setup';
import baiduUeditor from '@/plugins/baidu-ueditor';
import attachmentImg from "@/plugins/attachment-img";
import {changeDate} from "@/utils";
import {mapState} from "vuex";
export default {
  components: { baiduUeditor,attachmentImg},
  created() {
    this.init();
  },
  computed: {
    ...mapState(['storeId']),
  },
  data() {
    return {
      activeName:'storeappointmentstoreset',
      form: {
        days: '1',
        interval:'15',
        week:[],
        selectDay:'1',
        cancelTime:'60',
        minNum:'1',
        maxNum:'10',
        auto:'2',
        notice:'',
        introduction: '',
      },
      time:'',
    };
  },
  methods: {
    handleClick() {
      if (this.activeName == 'storeappointmentlist') {
        this.$router.push({path: 'storeappointmentlist'});
      } else if (this.activeName == 'storeappointmentorder') {
        this.$router.push({path: 'storeappointmentorder'});
      }else if (this.activeName == 'storeappointmentstoreset') {
        this.$router.push({path: 'storeappointmentstoreset'});
      }
    },
    getUeditor(content) {
      this.form.introduction = content;
    },
    enter(i=0){
      this.$refs['imgicon' + i].style.display = 'none'
    },
    leave(i=0){
      this.$refs['imgicon' + i].style.display = 'inline-block'
    },
    test(i = 0) {
      this.$refs['img' + i].dialogVisible = true
    },
    imgadd1(url) {
      this.form.icon = url.str
    },
    deleteMultiImage(item, index, type) {
      //typem没传是单图multigraph多图；index上传图片组件排序
      Array.prototype.remove = function (val) {
        var index = this.indexOf(val);
        if (index > -1) {
          this.splice(index, 1);
        }
      };
      if (type == 'multigraph') {
        switch (index) {
          case 2:
            this.form.icon.remove(item);
            break
        }
      } else {

        switch (index) {

          case 1:
            this.form.icon = '';
            break
        }
      }
    },
    async init() {
      const { data } = await getStoreConfig({ ident: 'appointment',storeId:this.storeId });
      this.form2 = this.form;
      this.form = Object.assign(this.form2, data);
      if(data.start){
        this.time =[data.start,data.end]
      }
    },
    submitForm() {
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          this.form.ident = 'appointment';
          this.form.identName = '预约设置';
          this.form.storeId = this.storeId;
          if(this.time){
            this.form.start = changeDate(this.time[0]);
            this.form.end = changeDate(this.time[1]);
          }
          const { msg } = await postStoreConfig(this.form);
          this.$baseMessage(msg, 'success');
          this.init();
        } else {
          return false;
        }
      });
    }
  }
};
</script>
